package com.csq.sso.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import com.csq.common.pojo.ReturnResult;
import com.csq.common.pojo.StatusCode;
import com.csq.common.util.JsonUtils;
import com.csq.manager.mapper.AccountMapper;
import com.csq.manager.mapper.EnterpriseMapper;
import com.csq.manager.pojo.Account;
import com.csq.manager.pojo.AccountExample;
import com.csq.manager.pojo.AccountExample.Criteria;
import com.csq.manager.pojo.Enterprise;
import com.csq.manager.pojo.EnterpriseExample;
import com.csq.sso.service.LoginService;

@Service
public class LoginServiceImpl implements LoginService {

	@Autowired
	private AccountMapper accountMapper;
	@Autowired
	private EnterpriseMapper enterpriseMapper;

	@Value("${SESSON_KEY}")
	private String SESSON_KEY;

	@Value("${SESSON_EXPIE}")
	private int SESSON_EXPIE;

	@Autowired
	private PoolJedisClient poolJedisClient;

	@Override
	public ReturnResult accountLogin(String email, String password) {
		// TODO Auto-generated method stub

		// 根据用户信息查询 用户

		AccountExample accountExample = new AccountExample();
		Criteria criteria = accountExample.createCriteria();

		// 手机号为用户名
		criteria.andEmailEqualTo(email);
		List<Account> selectByExample = accountMapper
				.selectByExample(accountExample);

		// 检验用户名
		if (selectByExample == null || selectByExample.size() == 0) {
			return ReturnResult.build(StatusCode.WRONGUSERNAME, "用户名不正确！！");
		}

		Account user = selectByExample.get(0);

		// 检验密码
		if (!DigestUtils.md5DigestAsHex(password.getBytes()).equals(
				user.getUserpass())) {
			System.out.println(DigestUtils.md5DigestAsHex(password.getBytes()));
			System.out.println(user.getUserpass());
			return ReturnResult.build(StatusCode.WRONGPASSWORD, "密码不正确！！");
		}

		// 生成UUID token
		/*
		 * String token = UUID.randomUUID().toString(); user.setUserpass(null);
		 */

		user.setUserpass(null);

		// token
		String token = DigestUtils.md5DigestAsHex(email.getBytes());

		// 写入redis 缓存
		poolJedisClient.set(SESSON_KEY + ":" + token,
				JsonUtils.objectToJson(user));

		poolJedisClient.expire(SESSON_KEY + ":" + token, SESSON_EXPIE);

		return ReturnResult.ok();
	}

	@Override
	public ReturnResult companyLogin(String email, String password) {

		// 根据用户信息查询 用户

		EnterpriseExample enterpriseExample = new EnterpriseExample();
		EnterpriseExample.Criteria criteria = enterpriseExample
				.createCriteria();

		// 手机号为用户名
		criteria.andWorkEmailEqualTo(email);
		List<Enterprise> selectByExample = enterpriseMapper
				.selectByExample(enterpriseExample);

		// 检验用户名
		if (selectByExample == null || selectByExample.size() == 0) {
			return ReturnResult.build(StatusCode.WRONGUSERNAME, "用户名不正确！！");
		}

		Enterprise enterprise = selectByExample.get(0);

		// 检验密码
		if (!DigestUtils.md5DigestAsHex(password.getBytes()).equals(
				enterprise.getPassword())) {
			System.out.println(DigestUtils.md5DigestAsHex(password.getBytes()));
			System.out.println(enterprise.getPassword());
			return ReturnResult.build(StatusCode.WRONGPASSWORD, "密码不正确！！");
		}

		// 生成UUID token
		/*
		 * String token = UUID.randomUUID().toString(); user.setUserpass(null);
		 */

		enterprise.setPassword(null);

		String token = DigestUtils.md5DigestAsHex(email.getBytes());

		// 写入redis 缓存
		poolJedisClient.set(SESSON_KEY + ":" + token,
				JsonUtils.objectToJson(enterprise));

		poolJedisClient.expire(SESSON_KEY + ":" + token, SESSON_EXPIE);
		return ReturnResult.ok();
	}

	@Override
	public ReturnResult adminLogin(String username, String password) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ReturnResult logout(String email) {
		String token = DigestUtils.md5DigestAsHex(email.getBytes());
		poolJedisClient.expire(SESSON_KEY + ":" + token, 0);
		return ReturnResult.ok();
	}

	@Override
	public ReturnResult checkAccountEmail(String email) {
		// TODO Auto-generated method stub

		AccountExample accountExample = new AccountExample();
		Criteria criteria = accountExample.createCriteria();

		System.out.println(email);
		// 手机号为用户名
		criteria.andEmailEqualTo(email);
		List<Account> selectByExample = accountMapper
				.selectByExample(accountExample);

		if (selectByExample != null && selectByExample.size() != 0) {
			System.out.println(selectByExample);
			return ReturnResult.build(StatusCode.USEREXIST, "邮箱已存在");
		}

		return ReturnResult.ok();
	}

	@Override
	public ReturnResult checkCompanyEmail(String email) {
		EnterpriseExample enterpriseExample = new EnterpriseExample();
		EnterpriseExample.Criteria createCriteria = enterpriseExample
				.createCriteria();

		System.out.println(email);
		// 手机号为用户名
		createCriteria.andWorkEmailEqualTo(email);
		List<Enterprise> selectByExample = enterpriseMapper
				.selectByExample(enterpriseExample);

		if (selectByExample != null && selectByExample.size() != 0) {
			System.out.println(selectByExample);
			return ReturnResult.build(StatusCode.USEREXIST, "邮箱已存在");
		}

		return ReturnResult.ok();
	}
}
